package com.bibrApp.difu.fragment.jieru_mvp;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.actvity.wait_delivery_mvp.WaitDeliveryActivity;
import com.bibrApp.difu.base.BaseFragment;

import java.util.List;

import butterknife.BindView;

public class JieruFragment extends BaseFragment<JieruPresenter> implements IJieruView {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private JiedaiManagerAdapter adapter;
    int pageSize = 10;
    int pageNum = 1;
    int type = 1;
    String status = "";

    public JieruFragment() {

    }

    public JieruFragment(int type, String status) {
        this.type = type;
        this.status = status;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_list;
    }

    @Override
    protected void initPresenter() {
        presenter = new JieruPresenter(getContext(), this);
        presenter.init();
    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void onDataResult(List<JiedaiManagerBean> list, boolean hasNext) {
        if (pageNum == 1 && (list == null || list.size() == 0) && adapter.getEmptyViewCount() == 0) {
            adapter.setEmptyView(View.inflate(getContext(), R.layout.empty_order, null));
            return;
        }
        adapter.addData(list);
        if (!hasNext) {
            adapter.loadMoreEnd();
        } else {
            adapter.loadMoreComplete();
        }

    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new JiedaiManagerAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(() -> {
            pageNum++;
            presenter.getDatas(type, status, pageNum, pageSize);
        }, recyclerView);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            Intent intent = new Intent(getContext(), WaitDeliveryActivity.class);
            intent.putExtra("item", adapter.getItem(position));
            startActivity(intent);
        });

        presenter.getDatas(type, status, pageNum, pageSize);

    }
}
